package com.free.fs.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.free.fs.controller.util.Result;
import com.free.fs.model.Summary;
import com.free.fs.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("Summary")
public class SummaryController {

    @Autowired
    SummaryService summaryService;
    @GetMapping
    Result getAll(){

        return new Result(true,summaryService.list());
    }

    @GetMapping("/{id}")
    Result getById(@PathVariable Integer id){
        return new Result(true,summaryService.getById(id));
    }

    @PostMapping
    Result save(@RequestBody Summary summary) throws IOException {
//        System.out.print(summary.toString()+"进入Post");
//        System.out.print(summary.getName());
        if(summary.getName().equals("123")) throw new IOException();
        boolean flag=summaryService.save(summary);
        return new Result(flag,flag?"添加成功":"添加失败");
    }

    @PutMapping
    Result update(@RequestBody Summary summary){
        return new Result(summaryService.updateById(summary));
    }

    @DeleteMapping("/{id}")
    Result delete(@PathVariable Integer id){
        return new Result(summaryService.removeById(id));
    }

//    @GetMapping("/{currentPage}/{pageSize}")
//    Result getPage(@PathVariable int currentPage,@PathVariable int pageSize){
//        IPage<Summary> page=summaryService.getPage(currentPage,pageSize);
//        if(currentPage > page.getPages())
//            page=summaryService.getPage((int)page.getPages(),pageSize);
//        return new Result(true,page);
//    }

    @GetMapping("/{currentPage}/{pageSize}")
    Result getPage(@PathVariable int currentPage,@PathVariable int pageSize,Summary summary){
        IPage<Summary> page=summaryService.getPage(currentPage,pageSize,summary);
        if(currentPage > page.getPages())
            page=summaryService.getPage((int)page.getPages(),pageSize,summary);
        return new Result(true,page);
    }


//    @RequestMapping("/submit")
//    public String checksubmit(@RequestParam String name, @RequestParam String gender, @RequestParam String birth,
//                              @RequestParam String pnative, @RequestParam String workplace_duty, @RequestParam String zhichen,
//                              @RequestParam String techang, @RequestParam String zuigaoxueli, @RequestParam String zuigaoxuewei,
//                              @RequestParam String school, @RequestParam String zuigaoxueli_onjob, @RequestParam String zuigaoxuewei_onjob,
//                              @RequestParam String school_onjob,
//                              @RequestParam String workplace,@RequestParam String ofphone,
//                              @RequestParam String category,@RequestParam String phone,@RequestParam String jttime,@RequestParam String hztime,@RequestParam String jt,@RequestParam String jttimeend,
//                              @RequestParam String lqtime,@RequestParam String lqtimeend,@RequestParam String jtm)
//    {
//        System.out.print(name+"-------收到");
//        return "收到";
//    }

//    @GetMapping("all")
//    List<Summary> getAll(){
//        return summaryService.list();
//    }
//
//    @GetMapping("find/{id}")
//    Summary getById(@PathVariable Integer id){
//        return summaryService.getById(id);
//    }
//
//    @PutMapping
//    boolean save(@RequestBody Summary summary){
//        return summaryService.save(summary);
//    }
//
//    @DeleteMapping("delete/{id}")
//    boolean delete(@PathVariable Integer id){
//        return summaryService.removeById(id);
//    }
//    @PostMapping("update")
//    boolean update(@RequestBody Summary summary){
//        return summaryService.updateById(summary);
//    }
}
